﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConquerServer_Basic
{
    public class MonsterSpawn
    {
        public uint SpawnID;
        public ushort MapID;
        public ushort X;
        public ushort Y;
        public ushort Xc;
        public ushort Yc;
        public uint MaxMobs;
        public ushort RestSecs;
        public uint SpawnAmount;
        public uint MobID;

        public static void Load()
        {
            int count = 0;
            IniFile cq_generator = new IniFile();
            foreach (string file in Directory.GetFiles(Database.DatabasePath + "\\cq_generator\\"))
            {
                cq_generator.FileName = file;
                MonsterSpawn spawn = new MonsterSpawn();
                spawn.SpawnID = cq_generator.ReadUInt32("cq_generator", "id", 0);
                spawn.MapID = cq_generator.ReadUInt16("cq_generator", "mapid", 0);
                spawn.X = cq_generator.ReadUInt16("cq_generator", "bound_x", 0);
                spawn.Y = cq_generator.ReadUInt16("cq_generator", "bound_y", 0);
                spawn.Xc = cq_generator.ReadUInt16("cq_generator", "bound_cx", 0);
                spawn.Yc = cq_generator.ReadUInt16("cq_generator", "bould_cy", 0);
                spawn.MaxMobs = cq_generator.ReadUInt32("cq_generator", "maxnpc", 0);
                spawn.RestSecs = cq_generator.ReadUInt16("cq_generator", "rest_secs", 0);
                spawn.SpawnAmount = cq_generator.ReadUInt32("cq_generator", "max_per_gen", 0);
                spawn.MobID = cq_generator.ReadUInt32("cq_generator", "npctype", 0);

                Kernel.MobSpawns.Add(spawn.SpawnID, spawn);
                count++;
            }

            Console.WriteLine("Spaws Loaded: " + Kernel.MobSpawns.Count);
        }

        public static void SpawnMobs()
        {
            foreach (MonsterSpawn spawn in Kernel.MobSpawns.Values)
            {
                Monster ThisMob = new Monster();
                foreach (Monster mob in Kernel.Mobs.Values)
                {
                    if (mob.ID == spawn.MobID)
                    {
                        ThisMob = mob;
                        break;
                    }
                }

                for (uint i = 0; i < spawn.SpawnAmount; i++)
                {

                    Entity newMob = new Entity(EntityFlag.Monster, null);
                    newMob.Action = ConquerAction.None;
                    newMob.Dead = false;
                    newMob.Defence = (ushort)ThisMob.Defence;
                    newMob.Dodge = (sbyte)ThisMob.Dodge;
                    newMob.Facing = (ConquerAngle)Kernel.Random.Next(0, 9);
                    newMob.Hitpoints = (Int32)ThisMob.Life;
                    newMob.MaxHitpoints = (Int32)ThisMob.Life;
                    newMob.Level = (byte)ThisMob.Level;
                    newMob.MagicAttack = ThisMob.AttackMin;
                    newMob.MaxAttack = ThisMob.AttackMax;
                    newMob.MinAttack = ThisMob.AttackMin;
                    newMob.MDefence = (ushort)ThisMob.MagicDefence;
                    newMob.Mesh = ThisMob.Lookface;
                    newMob.Name = ThisMob.Name;
                    newMob.UID = (uint)Kernel.Random.Next(400000, 500000);
                    while (Kernel.eMonsters.ContainsKey(newMob.UID))
                        newMob.UID = (uint)Kernel.Random.Next(400000, 500000);
                    newMob.MapID = spawn.MapID;
                    newMob.X = (ushort)Kernel.Random.Next(Math.Min(spawn.X, spawn.Xc), Math.Max(spawn.X, spawn.Xc));
                    newMob.Y = (ushort)Kernel.Random.Next(Math.Min(spawn.Y, spawn.Yc), Math.Max(spawn.Y, spawn.Yc));
                    Kernel.eMonsters.Add(newMob.UID, newMob);
                }

            }

            //Console.WriteLine("Monsters Spawned [{0}]", Kernel.eMonsters.Count);
        }

        //public static void Load2()
        //{
        //    string[] FSpawns = File.ReadAllLines(Database.DatabasePath + @"//Misc//MobSpawns.ini");
        //    ushort mobspawn = 1;
        //    foreach (string Spawn in FSpawns)
        //    {
        //        if (Spawn[0] == '*') return;
        //        string[] SpawnInfo = Spawn.Split(' ');
        //        MonsterSpawn spawn = new MonsterSpawn();
        //        spawn.SpawnID = mobspawn;
        //        spawn.MobID = uint.Parse(SpawnInfo[0]);
        //        spawn.SpawnAmount = uint.Parse(SpawnInfo[1]);
        //        spawn.MapID = ushort.Parse(SpawnInfo[2]);
        //        spawn.X = ushort.Parse(SpawnInfo[3]);
        //        spawn.Y = ushort.Parse(SpawnInfo[4]);
        //        spawn.Xc = ushort.Parse(SpawnInfo[5]);
        //        spawn.Yc = ushort.Parse(SpawnInfo[6]);
        //        Kernel.MobSpawns.Add(spawn.SpawnID, spawn);
        //        mobspawn++;
        //    }
        //    Console.WriteLine("Spawns Loaded " + mobspawn--);
        //}
    }
}
